package com.XDApp.xdbase.activitys;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.XDApp.xdbase.BaseActivity;
import com.XDApp.xdbase.ConstantValue;
import com.XDApp.xdbase.R;
import com.XDApp.xdbase.utils.Logger;

public class PaperActivity extends BaseActivity implements
		OnPageChangeListener, OnClickListener {

	private static final String TAG = "PaperActivity";
	private static final int POINT_WIDTH = 15;
	private static final int POINT_HEIGHT = 15;
	private ViewPager mViewPager;
	// private TextView tvDescription;
	private LinearLayout llPoints;

	private List<ImageView> imageViewList;

	private int previousSelectPosition = 0;
	private Button btn_loadui;

	/**
	 * 初始化图片资源文件
	 * 
	 * @return
	 */
	private int[] getImageResIDs() {
		switch (ConstantValue.GUIDECOUNT) {
		case 3:
			return new int[] { R.drawable.guide_page_1, R.drawable.guide_page_2, R.drawable.guide_page_3, };
		case 4:
			return new int[] { R.drawable.guide_page_1, R.drawable.guide_page_2, R.drawable.guide_page_3,
					R.drawable.guide_page_2 };
		case 5:
			return new int[] { R.drawable.guide_page_1, R.drawable.guide_page_2, R.drawable.guide_page_3,
					R.drawable.guide_page_2, R.drawable.guide_page_5 };
		case 6:
			return new int[] { R.drawable.guide_page_1, R.drawable.guide_page_2, R.drawable.guide_page_3,
					R.drawable.guide_page_2, R.drawable.guide_page_5, R.drawable.guide_page_6 };
		default:
			return new int[] { R.drawable.guide_page_1, R.drawable.guide_page_2, R.drawable.guide_page_3};
		}
	}

	@Override
	protected void initViews() {
		setContentView(R.layout.activity_pager);
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		btn_loadui = (Button) findViewById(R.id.btn_loadui);
		// tvDescription = (TextView) findViewById(R.id.tv_image_description);
		llPoints = (LinearLayout) findViewById(R.id.ll_points);
		prepareData();

	}

	@Deprecated
	private void prepareData() {
		imageViewList = new ArrayList<ImageView>();
		int[] imageResIDs = getImageResIDs();
		// imageDescriptions = getImageDescription();

		ImageView iv;
		View view;
		for (int i = 0; i < imageResIDs.length; i++) {
			iv = new ImageView(this);
			iv.setBackgroundResource(imageResIDs[i]);
			imageViewList.add(iv);

			// 添加点view对象
			view = new View(this);
			view.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.point_background));
			// 点大小设置
			LayoutParams lp = new LayoutParams(POINT_WIDTH, POINT_HEIGHT);
			lp.leftMargin = 10;
			view.setLayoutParams(lp);
			view.setEnabled(false);
			llPoints.addView(view);
		}
	}

	@Override
	protected void initEvents() {

		ViewPagerAdapter adapter = new ViewPagerAdapter();
		mViewPager.setAdapter(adapter);
		mViewPager.setOnPageChangeListener(this);
		btn_loadui.setOnClickListener(this);
		// mViewPager.set
		llPoints.getChildAt(previousSelectPosition).setEnabled(true);
	}

	class ViewPagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return imageViewList.size();
		}

		/**
		 * 判断出去的view是否等于进来的view 如果为true直接复用
		 */
		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		/**
		 * 销毁预加载以外的view对象, 会把需要销毁的对象的索引位置传进来就是position
		 */
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(imageViewList.get(position));
		}

		/**
		 * 创建一个view
		 */
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(imageViewList.get(position));
			return imageViewList.get(position);
		}
	}

	//
	// //SCROLL_STATE_IDLE
	// SCROLL_STATE_DRAGGING
	// SCROLL_STATE_SETTLING
	@Override
	public void onPageScrollStateChanged(int state) {
		if (state == ViewPager.SCROLL_STATE_DRAGGING) {
			btn_loadui.setVisibility(View.GONE);
		} else {
			// btn_loadui.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// Logger.i(TAG, " :"+arg0+" :"+arg1+":"+arg2);

	}

	@Override
	public void onPageSelected(int position) {
		if (position == (imageViewList.size() - 1)) {
			// TODO
			// 显示进入的 按钮
			btn_loadui.setVisibility(View.VISIBLE);
			llPoints.setVisibility(View.INVISIBLE);
		} else {
			btn_loadui.setVisibility(View.INVISIBLE);
			llPoints.setVisibility(View.VISIBLE);
		}
		// 改变图片的描述信息
		// 切换选中的点
		llPoints.getChildAt(previousSelectPosition).setEnabled(false); // 把前一个点置为normal状态
		llPoints.getChildAt(position % imageViewList.size()).setEnabled(true); // 把当前选中的position对应的点置为enabled状态
		previousSelectPosition = position % imageViewList.size();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_loadui:
			startActivity(MainActivity.class);
			finish();
			break;
		default:
			break;
		}
	}

}
